//
//  TZAdController.m
//  BSBDJ
//
//  Created by user on 16/5/28.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZAdController.h"
#import "TZTabbarController.h"
#import "TZAdItem.h"

#define Code2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"



@interface TZAdController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *cover_ImageV;
@property (weak, nonatomic) IBOutlet UIView *placher_View;
@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;
@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, assign) int time;

@property (nonatomic, weak) UIImageView *adImageView;
/** 广告模型*/
@property (nonatomic ,strong) TZAdItem *item;


@end

@implementation TZAdController

- (UIImageView *)adImageView{
    if (_adImageView == nil) {
        UIImageView *adImageView = [UIImageView new];
        adImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickAd)];
        tap.delegate = self;
        [adImageView addGestureRecognizer:tap];
        
        [self.placher_View insertSubview:adImageView belowSubview:self.jumpBtn];
        _adImageView = adImageView;
    }
    return _adImageView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //时间设置为3
    self.time = 3;
    //广告
    [self requestAd];
    //图片
    [self setupImage];
    //计时器
    [self setupTimer];
    
}

#pragma mark --------------
#pragma mark 定时器
- (void)setupTimer{
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
// 倒计时
- (void)updateTimer{
    NSString *timeStr = [NSString stringWithFormat:@" 跳过(%d) ",--self.time];
    if (self.time <= 0) {
        [self jumpClick:nil];
    }
    [self.jumpBtn setTitle:timeStr forState:0];
}

#pragma mark --------------
#pragma mark 请求广告
- (void)requestAd{
    MJWeakSelf
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = Code2;
    [HttpRequestList requestAdvertising:@"http://mobads.baidu.com/cpro/ui/mads.php" paramaters:parameters success:^(TZAdItem *adItem) {
        weakSelf.item = adItem;
        CGFloat adWidth = TZScreenW;
        CGFloat adHeight = adWidth *adItem.h / adItem.w;
        
        [self.adImageView sd_setImageWithURL:[NSURL URLWithString:adItem.w_picurl]];
        self.adImageView.frame = CGRectMake(0, 0, adWidth, adHeight);
        
    } :^(NSError *error) {
        NSLog(@"%@",error);
    }];
}

//点击广告
- (void)clickAd{
    // 跳转到广告界面
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:[NSURL URLWithString:_item.ori_curl]]) {
        
        [app openURL:[NSURL URLWithString:_item.ori_curl]];
    }
}

#pragma mark --------------
#pragma mark 设置图片
- (void)setupImage{
    // 根据不同屏幕高度 加载不同图片
    UIImage *image = nil;
    
    if (iphone6P) { // 6P
        image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    } else if (iphone6) {
        image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iphone5) {
        image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    } else if (iphone4) {
        image = [UIImage imageNamed:@"LaunchImage-700"];
    }
    
    _cover_ImageV.image = image;
}

//跳转按钮
- (IBAction)jumpClick:(UIButton *)sender {
    
    TZTabbarController *tab = [[TZTabbarController alloc] init];
    [self.timer invalidate];
    
    CATransition *anim = [CATransition animation];
    anim.type = @"rippleEffect";
    anim.duration = 1.0;
    [[UIApplication sharedApplication].keyWindow.layer addAnimation:anim forKey:nil];
    [UIApplication sharedApplication].keyWindow.rootViewController = tab;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}

- (void)dealloc{
    NSLog(@"delloc----AD");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
